<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
      <div class="box">
        <div class="left"></div>
        <div class="right">
          <div class="logo">
            <img src="../../assets/login_logo.png" />
          </div>

          <div class="title-container">
            <h3 class="title">欢迎登录POLARIS BAR后台管理</h3>
          </div>

          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="账号"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </el-form-item>

          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="密码"
                name="password"
                tabindex="2"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>

          <el-button :loading="loading" type="primary" class="submit-button" @click.native.prevent="handleLogin">登录</el-button>
        </div>
        <img class="dian1" src="../../assets/login_dian.png" />
        <img class="dian2" src="../../assets/login_dian.png" />
        <img class="dian3" src="../../assets/login_dian.png" />
      </div>
    </el-form>
  </div>
</template>

<script>
import { validUsername, validPassword } from '@/utils/myvalidate'

export default {
  name: 'Login',
  components: { },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('账号必须是6个数字或字母'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      var valRes = validPassword(value)
      if (valRes.code) {
        callback(new Error(valRes.msg))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'dengshikj',//dengshikj || 17608457936
        password: '123456'//123456 || chenke68
      },
      loginRules: {
        // username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        // password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    //  键盘输入限制
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    //  是否密码可见
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    //  执行登录操作
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then((res) => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #000;

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {
//     color: $cursor;
//   }
// }

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      // color: $light_gray;
      color: #000;
      height: 47px;
      // caret-color: $cursor;

      // &:-webkit-autofill {
      //   box-shadow: 0 0 0px 1000px rgba(0,0,0,1) inset !important;
      //   -webkit-text-fill-color: $cursor !important;
      // }
    }
  }

  .el-form-item {
    width: 364px;
    height: 52px;
    background: #FDFDFD;
    border: 2px solid #DCDCDC;
    border-radius: 26px;
    margin: 0 auto 24px;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100vh;
  width: 100%;
  background-color: $bg;
  background-image: url('../../assets/login_background.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  overflow: hidden;
  .dian1{
    width: 107px;
    position: absolute;
    left: -256px;
    bottom: -67px;
  }
  .dian2{
    width: 210px;
    position: absolute;
    right: -130px;
    bottom: -160px;
  }
  .dian3{
    width: 107px;
    position: absolute;
    right: -257px;
    bottom: 200px;
  }

  @media only screen and (max-width: 860px) {
    .login-form{
      width: 95%;
      overflow: hidden;
      .box{
        width: 90% !important;
        .left{
          display: none;
        }
        .right{
          width: 90% !important;
          margin: 0 auto;
        }
        .el-form-item{
          width: 90% !important;
        }
        .submit-button{
          width: 90% !important;
        }
      }
    }
  }
  @media only screen and (max-width: 1500px) {
    .dian1,.dian2,.dian3{
      display: none;
    }
  }

  .login-form {
    position: relative;
    max-width: 984px;
    width: 100%;
    height: 573px;
    padding: 160px 35px 0;
    margin: 0 auto;
    // overflow: hidden;
    border: 2px solid #FFFFFF;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    background-color: rgba(255,255,255,0.3);
    .box{
      width: 908px;
      height: 497px;
      background: #FFFFFF;
      border-radius: 10px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%,-50%);
      display: flex;
      .left,.right{
        width: 50%;
        height: 100%;
      }
      .left{
        background-image: url(../../assets/form_left_background.png);
        background-repeat: no-repeat;
        background-position: center;
      }
      .right{
        text-align: center;
        .logo{
          margin-top: 48px;
          margin-bottom: 20px;
        }
      }
      .submit-button{
        width: 364px;
        height: 52px;
        background: #E82F5F;
        border-radius: 26px;
        border: none;
        font-size: 20px;
        font-weight: 400;
        color: #FFFFFF;
        text-shadow: 0px 2px 2px rgba(0, 0, 0, 0.5);
      }
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 21px;
      font-weight: bold;
      color: #E82F5F;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
